package com.mebs.edi.datalayer.jpa;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.TypedQuery;

import com.mebs.edi.datalayer.LegStatusChangeDao;
import com.mebs.edi.model.edi.EdiIsa;
import com.mebs.model.logistics.Leg;
import com.mebs.model.logistics.LegStatusChange;

public class JpaLegStatusChangeDao extends JpaGenericDao implements LegStatusChangeDao {

	public LegStatusChange getLastStatusChange(Leg l) {
EntityManager em = getEntityManager();
		
		String queryString = 
			"select c from LegStatusChange c where c.leg = :leg order by c.date desc";
		
		TypedQuery<LegStatusChange> query = em
			.createQuery(queryString, LegStatusChange.class)
			.setParameter("leg", l);
		
		List<LegStatusChange> lscList = query.getResultList();
		
		return (lscList == null || lscList.isEmpty()) ? null : lscList.get(0);
	}

}
